package com.kitty.game.fight.pet;

import com.kitty.game.fight.bean.Fight;
import com.kitty.game.fight.bean.FightObject;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.fight.service.FightMessageService;
import com.kitty.game.fight.util.FightMessageUtil;
import com.kitty.game.utils.StatusConst;

/**
 * 移花接木
 * 对自己使用成功后，可以使被施法对象用法力来暂时的提升气血上限。技能等级越高，效果持续的回合数越多
 */
public class YiHuaJieMuProcessor extends PetSkillProcessor {

    @Override
    public void trigger(Fight fight, FightObject effector, FightObject target, int skillLevel, Object... args) {
        // 只能对自己
        if (effector != target) {
            return;
        }
        int status = bindingStatus();
        int lastRound = (int)(skillLevel * 0.025);
        if (lastRound <= 0) {
            lastRound = 1;
        }
        // 额外加1是因为回合开始的时候会自动减一，奇葩的设定，后续看怎么改
        effector.setStatus(status, lastRound + 1);
    }

    @Override
    public Integer effect(Fight fight, FightObject effector, FightObject target, Object... args) {
        int hurt = (int) args[0];
        if (fight.isSameSide(effector, target)) {
            return hurt;
        }

        int status = bindingStatus();
        int score = target.getStatusValue(status);
        if (score <= 0) {
            return hurt;
        }
        int currMp = target.getCurrMana();
        int costMp = 0;
        if (currMp >= hurt) {
            costMp = hurt;
            hurt = 0;
        } else {
            hurt -= currMp;
            costMp = currMp;
        }

        if (costMp > 0) {
            target.reduceMp(costMp);
            FightMessageService fightMessageService = SpringUtils.getFightMessageService();
            FightMessageUtil.sendFightPackage(fight, fightMessageService.decreaseMana(target.getId(), target.getId(), costMp, 0));
            FightMessageUtil.sendFightPackage(fight, fightMessageService.updateObjectInfo(target));
        }

        return hurt;
    }

    public int bindingStatus() {
        return StatusConst.STATUS_ADD_LIFE_BY_MANA;
    }
}
